package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.mapper.TagMapper;
import com.tarena.lbs.basic.pojo.dto.TagQuery;
import com.tarena.lbs.basic.pojo.po.TagLibraryPO;
import com.tarena.lbs.basic.pojo.vo.TagLibraryVO;
import com.tarena.lbs.basic.pojo.vo.TagVO;
import com.tarena.lbs.basic.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    TagMapper tagMapper;

    @Override
    public Map<String, List<TagVO>> selectArticleTags() {
        TagQuery tagQuery = new TagQuery();
        tagQuery.setTagType(0);

        List<TagLibraryPO> allTags = tagMapper.select(tagQuery);

        Map<Integer, String> idToNameMap = new HashMap<>();
        for (TagLibraryPO po : allTags) {
            idToNameMap.put(po.getId(), po.getTagName());
        }

        Map<String, List<TagVO>> resultMap = new HashMap<>();

        for (TagLibraryPO po : allTags) {
            if (po.getTagParentId() == null || po.getTagParentId() == 0) {
                resultMap.putIfAbsent(po.getTagName(), new ArrayList<>());
            } else {
                String parentName = idToNameMap.get(po.getTagParentId());
                if (parentName != null) {
                    TagLibraryVO tagLibraryVO = new TagLibraryVO();
                    BeanUtils.copyProperties(po,tagLibraryVO);
                    resultMap.computeIfAbsent(parentName, k -> new ArrayList<>()).add(new TagVO(parentName, tagLibraryVO));
                }
            }
        }

        return resultMap;
    }

    @Override
    public Map<String, List<TagVO>> selectUserTags() {
        TagQuery tagQuery = new TagQuery();
        tagQuery.setTagType(3);

        List<TagLibraryPO> allTags = tagMapper.select(tagQuery);

        Map<Integer, String> idToNameMap = new HashMap<>();
        for (TagLibraryPO po : allTags) {
            idToNameMap.put(po.getId(), po.getTagName());
        }

        Map<String, List<TagVO>> resultMap = new HashMap<>();

        for (TagLibraryPO po : allTags) {
            if (po.getTagParentId() == null || po.getTagParentId() == 0) {
                resultMap.putIfAbsent(po.getTagName(), new ArrayList<>());
            } else {
                String parentName = idToNameMap.get(po.getTagParentId());
                if (parentName != null) {
                    TagLibraryVO tagLibraryVO = new TagLibraryVO();
                    BeanUtils.copyProperties(po,tagLibraryVO);
                    resultMap.computeIfAbsent(parentName, k -> new ArrayList<>()).add(new TagVO(parentName, tagLibraryVO));
                }
            }
        }

        return resultMap;
    }

    @Override
    public Map<String, List<TagVO>>  selectGroupTags() {
        TagQuery tagQuery = new TagQuery();
        tagQuery.setTagType(3);

        List<TagLibraryPO> allTags = tagMapper.select(tagQuery);

        Map<Integer, String> idToNameMap = new HashMap<>();
        for (TagLibraryPO po : allTags) {
            idToNameMap.put(po.getId(), po.getTagName());
        }

        Map<String, List<TagVO>> resultMap = new HashMap<>();

        for (TagLibraryPO po : allTags) {
            if (po.getTagParentId() == null || po.getTagParentId() == 0) {
                resultMap.putIfAbsent(po.getTagName(), new ArrayList<>());
            } else {
                String parentName = idToNameMap.get(po.getTagParentId());
                if (parentName != null) {
                    TagLibraryVO tagLibraryVO = new TagLibraryVO();
                    BeanUtils.copyProperties(po,tagLibraryVO);
                    resultMap.computeIfAbsent(parentName, k -> new ArrayList<>()).add(new TagVO(parentName, tagLibraryVO));
                }
            }
        }

        return resultMap;
    }
}
